package com.isoft.illustrationsdraw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isoft.illustrationsdraw.entity.Tag;
import com.isoft.illustrationsdraw.mapper.TagMapper;
import com.isoft.illustrationsdraw.service.TagService;

import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Override
    public Long getTagIdByName(String name) {
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        Tag tag = getOne(wrapper);
        
        return (tag != null) ? tag.getId() : null;  // 返回用户 ID，如果用户不存在返回 null
    }

    @Override
    public List<Tag> getRandomTagsExcluding(List<Long> excludeIds, int limit) {
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
    
        // 如果 excludeIds 不为空且不为空列表，添加 NOT IN 条件
        if (excludeIds != null && !excludeIds.isEmpty()) {
            queryWrapper.notIn("id", excludeIds);  // 排除指定的标签 ID
        }
    
        queryWrapper.orderByAsc("RAND()")    // 使用数据库的随机排序（MySQL 示例）
                    .last("LIMIT " + limit);  // 限制查询结果的数量
    
        return list(queryWrapper);  // 返回多个随机标签
    }
    
}
